package com.smarttable.ysd;

import android.content.ComponentName;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.smarttable.ysd.floatwindow.FxService;
import com.smarttable.ysd.floatwindow.MuteService;
import com.smarttable.ysd.floatwindow.MuteWindow;
import com.smarttable.ysd.floatwindow.MuteWindow2;
import com.smarttable.ysd.floatwindow.RotationWindow;
import com.smarttable.ysd.floatwindow.RotationWindow2;
import com.umeng.analytics.MobclickAgent;

import java.util.Calendar;

import wei.mark.standout.StandOutWindow;

public class MainActivity extends AppCompatActivity {

    RelativeLayout gameLayout ;
    RelativeLayout newsLayout ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        gameLayout = (RelativeLayout)this.findViewById(R.id.game_layout);
        newsLayout = (RelativeLayout)this.findViewById(R.id.news_layout);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
//        StandOutWindow
//                .show(this, MuteWindow.class, 1);


        Intent intent2 = new Intent(getApplicationContext(), MuteService.class);
        startService(intent2);
        Intent intent1 = new Intent(getApplicationContext(), FxService.class);
        startService(intent1);
//        StandOutWindow
//                .show(this, RotationWindow.class, 2);
//        StandOutWindow
//                .show(this, MuteWindow2.class, 3);
//        StandOutWindow
//                .show(this, RotationWindow2.class, 4);
        gameLayout.setVisibility(View.VISIBLE);
        newsLayout.setVisibility(View.VISIBLE);
        if(App.forbidEntertainment){
            Calendar calendar = Calendar.getInstance();
            int hour = calendar.get(Calendar.HOUR_OF_DAY);
            if(hour > 9  && hour < 19){
                gameLayout.setVisibility(View.GONE);
                newsLayout.setVisibility(View.GONE);
            }
        }

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    public void userManager(View view) {
        Intent intent = new Intent();
        intent.setClass(this,UserManagerActivity.class);
        this.startActivity(intent);
    }

    public void todayNews(View view) {
        if(App.forbidEntertainment){
            Calendar calendar = Calendar.getInstance();
            int hour = calendar.get(Calendar.HOUR_OF_DAY);
            if(hour > 9  && hour < 19){
                Toast.makeText(this, "特定时间段，本功能暂未开放！", Toast.LENGTH_SHORT)
                        .show();
                return ;
            }
        }
        gotoCategoryPage(2);
    }

    public void gamePlay(View view) {
//        Toast.makeText(this,"游戏娱乐",Toast.LENGTH_SHORT).show();
        if(App.forbidEntertainment){
            Calendar calendar = Calendar.getInstance();
            int hour = calendar.get(Calendar.HOUR_OF_DAY);
            if(hour > 9  && hour < 19){
                Toast.makeText(this, "特定时间段，本功能暂未开放！", Toast.LENGTH_SHORT)
                        .show();
                return ;
            }
        }
        gotoCategoryPage(1);

    }

    public void selfService(View view) {
        Intent itnDiancan = new Intent(Intent.ACTION_MAIN, null);
        itnDiancan.addCategory(Intent.CATEGORY_LAUNCHER);
        try {
            ComponentName cn = new ComponentName(
                    "com.tcsl",
                    "com.tcsl.SuperMain");
            itnDiancan.setComponent(cn);
            startActivity(itnDiancan);
        } catch (Exception e) {
            try {
                ComponentName cn = new ComponentName(
                        "com.lemontree.selforder",
                        "com.lemontree.selforder.activity.MainActivity");
                itnDiancan.setComponent(cn);
                startActivity(itnDiancan);
            } catch (Exception e1) {
                try {
                    ComponentName cn = new ComponentName(
                            "com.fzpos.selfhelp",
                            "com.fzpos.selfhelp.activity.MainActivity");
                    itnDiancan.setComponent(cn);
                    startActivity(itnDiancan);
                } catch (Exception e2) {
                    Toast.makeText(this, "点餐软件不存在，请管理人员进行安装！！", Toast.LENGTH_SHORT)
                            .show();
                }
            }
        }
    }

    @Override
    public void onBackPressed() {

    }

    /*
     * category == 1 代表游戏btn
     * category == 2 代表视频btn
     */
    private void gotoCategoryPage(int category){
        Intent intent = new Intent();
        intent.setClass(this, CategoryDetailActivity.class);
        Bundle extras = new Bundle();
        extras.putInt("CATEGORY", category);
        intent.putExtras(extras);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
    }

}
